from CvPythonExtensions import *

import PlanetfallSpells
from PlanetfallSpells import PlanetfallSpellImmediateBase

gc = CyGlobalContext()

class ConvertToRover(PlanetfallSpellImmediateBase):
    def __init__(self):
        PlanetfallSpellImmediateBase.__init__(self)

        self.pCastingUnits = []
        self.pCastingUnits.append( gc.getInfoTypeForString("UNIT_UNITY_SUPPLIES") )

        self.iConvertToUnit = gc.getInfoTypeForString("UNIT_SCOUT_ROVER")


    def canUnitCast(self, pCastingUnit, pPlot, bVisible):
        if not pCastingUnit.getUnitType() in self.pCastingUnits:
            return False

        return True

    def startSpellResults(self, pCastingGroup, pTarget):
        iPlayer = pCastingGroup.getOwner()
        pPlayer = gc.getPlayer(iPlayer)
        pPlot = pCastingGroup.plot()

        for pUnit in pCastingGroup.getUnits():
            if self.canUnitCast(pUnit, pPlot, True):
                pNewUnit = pPlayer.initUnit(self.iConvertToUnit, pPlot.getX(), pPlot.getY(), UnitAITypes.NO_UNITAI)
                pUnit.kill(True, PlayerTypes.NO_PLAYER)
